function XX = Ex4(x)
% K = 3; L = 1
    global alpha Sigma0 I qvec sig0; 
    K = 3; L = 1; weight=[x,1,1];
    vard = weight*Sigma0*weight'; 
    if (vard > 10^(-5))
        weight = weight./sqrt(vard);
        WMat = [eye(K,K);weight]; Sigmapl = WMat*Sigma0*WMat';
        Lamini = alpha.*[diag(diag(Sigma0)),zeros(K,L);zeros(L,K),alpha*Sigma0(1,1).*eye(L,L)]./(I-2)+eye(K+L,K+L);
        diff = 1000; count = 0;
        while (diff > 10^(-5)) && (count < 30)
            CMat = pinv(Lamini')./(I-1);
            BMat = pinv((1-sig0).*(alpha.*Sigmapl+Lamini)+sig0.*(I-1).*Lamini')*(alpha.*(WMat*Sigma0));
            Ret1 = (alpha.*Sigmapl+Lamini)*(BMat*BMat');
            Ret2 = (BMat*BMat');
            Ret1d = diag(diag(Ret1));
            Ret2d = diag(diag(Ret2));
            Lamnew = (Ret1d*pinv(Ret2d))'./(I-1);

            diff = sqrt(sum(sum((Lamini-Lamnew).^2)));
            Lamini = Lamnew;
            count = count + 1;
        end
        diff1 = diff;

        if (diff > 10^(-5))
            Cini = pinv(Lamini')./(I-1);
            diff = 1000; count = 0;
            while (diff > 10^(-5)) && (count < 30)
                Laminter = pinv(Cini')./(I-1);
                BMat = pinv((1-sig0).*(alpha.*Sigmapl+Laminter)+sig0.*(I-1).*Laminter')*(alpha.*(WMat*Sigma0));
                Ret1 = (alpha.*Sigmapl+Laminter)*(BMat*BMat');
                Ret2 = (BMat*BMat');
                Ret1d = diag(diag(Ret1));
                Ret2d = diag(diag(Ret2));
                Cnew = pinv(Ret1d*pinv(Ret2d));

                diff = sqrt(sum(sum((Cini-Cnew).^2)));
                Cini = Cnew;
                count = count + 1;
            end    
            diff2 = diff;
        else
            diff2 = 10^6;
        end    

        if (diff1 > diff2) 
            LamMat = pinv(Cini')./(I-1); 
            err2 = diff2;
        else 
            LamMat = Lamini;  
            err2 = diff1;
        end 

        if (err2 < 10^(-5))
            LamProj = pinv(WMat'*pinv(LamMat)*WMat);
            retvalue = qvec*(alpha.*Sigma0)*WMat'*pinv(alpha.*Sigmapl+LamMat')*(0.5.*alpha.*Sigmapl+0.5.*LamMat+0.5.*LamMat')*pinv(alpha.*Sigmapl+LamMat)*WMat*(alpha.*Sigma0)*qvec';
        else
            retvalue = -10^6;
        end
    else
        retvalue = -10^6;
    end
    
XX = qvec*(0.5.*alpha.*Sigma0)*qvec'-retvalue; 

